import Vue from 'vue'
import Router from 'vue-router'
import {Message} from "element-ui";
Vue.prototype.$message=Message;
Vue.use(Router)
const originalPush = Router.prototype.push

Router.prototype.push = function push (location) {

return originalPush.call(this, location).catch(err => err)

}
const routes=[
  {
    path: '',
    redirect:'/login'
  },
  {
    path:'/login',
    component:()=>import('../components/Login')
  },
  {
    path: '/home',
    component:()=>import('../components/Home/index.vue'),
    children:[
      {
        path: 'purchase',
        meta: { title: '采购管理'},
        redirect: '/purchase/supplierMag',
        component:()=>import('../components/purchase'),
        children:[
          {
            path: 'supplierMag',
            component:()=>import('../components/purchase/Supplier'),
            meta: { title: '供应商信息管理'}
          },{
            path: 'dragInfoMag',
            component:()=>import('../components/purchase/DragInfo'),
            meta: { title: '供应商信药品信息管理息管理'}
          },{
            path: 'purchaseListMag',
            component:()=>import('../components/purchase/PurchaseList'),
            meta: { title: '采购单添加'}
          },  {
            path: 'purchaseInfoMag',
            component:()=>import('../components/purchase/PurchaseInfo'),
            meta: { title: '药品采购明细管理'}
          }, 
    
        ]
      },{
        path: 'sale',
        meta: { title: '销售管理'},
        redirect: '/sale/createSaleOrder',
        component:()=>import('../components/sale'),
        children:[
          {
            path: 'createSaleOrder',
            component:()=>import('../components/sale/AddSaleList'),
            meta: { title: '销售单添加'},
          }, {
            path: 'getSaleInfo',
            component:()=>import('../components/sale/SaleInfo'),
            meta: { title: '销售明细管理'},
          },
    
        ]
      },{
        path: 'pharmacycount',
        meta: { title: '药店统计管理'},
        redirect: '/pharmacycount/getInventory',
        component:()=>import('../components/pharmacycount'),
        children:[
          {
            path: 'getInventory',
            component:()=>import('../components/pharmacycount/Inventory'),
            meta: { title: '入库统计'},
          },{
            path: 'SaleCount',
            component:()=>import('../components/pharmacycount/SaleCount'),
            meta: { title: '销售统计'},
          },{
            path: 'InStorage',
            component:()=>import('../components/pharmacycount/InStorage'),
            meta: { title: '库存查询'},
          },
    
        ]
      },{
        path: 'PersonalMag',
        meta: { title: '人事管理'},
        redirect: '/PersonalMag/EmployeeAdd',
        component:()=>import('../components/PersonalMag'),
        children:[
          {
            path: 'EmployeeAdd',
            component:()=>import('../components/PersonalMag/EmployeeAdd'),
            meta: { title: '员工添加'},
          },{
            path: 'employeeMana',
            component:()=>import('../components/PersonalMag/EmployeeMana'),
            meta: { title: '员工管理'},
          },
        ]
      },{
        path: 'print',
        meta: { title: '打印管理'},
        redirect: '/print/inOrder',
        component:()=>import('../components/print'),
        children:[
          {
            path: 'inOrder',
            component:()=>import('../components/print/InOrders'),
            meta: { title: '入库单'},
          },{
            path: 'outOrder',
            component:()=>import('../components/print/OutOrders'),
            meta: { title: '出库单'},
          },
    
        ]
      },{
        path: 'System',
        meta: { title: '系统管理'},
        redirect:'System/getPassChange',
        component:()=>import('../components/System/PassChange'),
        children:[
          {
            path: 'PassChange',
            component:()=>import('../components/System/PassChange'),
            meta: { title: '入库单'},
          }
        ]
      },{
        path:'',
        component:()=>import('../components/Home/Home_Index_Page/home.vue')
      }
    ]
  },
]

const router=new Router({
  routes,
  mode:'history',
})
router.beforeEach((to,from,next)=>{ // guard 
  console.log(to,from);
  if (to.meta.title) {
    document.title =`小药店 |  ${ to.meta.title}` || '管理后台'
  }
  next()

  // 如果用户访问登录页面，直接访问
  if(to.path==='/login'){
    return next();
  }
  // 如果用户访问主页但是没有 token 权限， 强制跳到 登录页面
  // const tokenStr=window.sessionStorage.getItem("token");
  // if(!tokenStr){
  //   // Vue.$message.info('请先登录');
  //   return next('/login')
  // }
  next();
})


export default router;
